package org.winexpress

import grails.converters.JSON
import org.codehaus.groovy.grails.web.json.JSONObject
import org.codehaus.groovy.grails.web.json.JSONArray

import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine
import org.apache.http.message.BasicHeader;
import org.apache.http.protocol.HTTP;


class WebServiceController {

    // GET
    def getCustomerByID = {
        
        def id = params['customerId']
       
        // consume winexpress server web service
        def txt = new URL("http://localhost:8080/WineXpress/customer/" + id).text
        
        if (txt.getAt(0) == '{') {
            JSONObject o = new JSONObject(txt)
            if (o.has("404")) {
                if (o.get("404") == true) {
                    println 'No such customer.<br/><br/>'
                    return null
                }
            }
            else {
                Customer c = new Customer()
                c.id = o.get("id")
                c.username = o.get("username")
                c.password = o.get("password")
                c.lastLogin = o.get("lastLogin")
                c.activationStatus = o.get("activationStatus")
                c.lastName = o.get("lastName")
                c.firstName = o.get("firstName")
                c.birthdate = o.get("birthdate")
                c.email = o.get("email")
                c.blkHouseNo = o.get("blkHouseNo")
                c.streetName = o.get("streetName")
                c.unitNo = o.get("unitNo")
                c.postalCode = o.get("postalCode")
                c.telNo = o.get("telNo")
                c.country = o.get("country")
                
                return c
            }
        }
        else {
            println 'No such customer.<br/><br/>'
            return null
        }
    }
    
    
    // POST
    def createShoppingCart = {
        
        println "HERE IN WEB SERVICE :: createShoppingCart"

        // get form values
        def custId = params['customerId']
        
        // create a JSONObject to send to winexpress server
        JSONObject o = new JSONObject()
        o.put("customerId",custId)
        
        // to create use POST
        HttpClient httpclient = new DefaultHttpClient()
        HttpResponse response
        // consume winexpress server web service
        HttpPost httppost = new HttpPost("http://localhost:8080/WineXpress/customer/" + custId + "/cart/") // WineXpress Server's URI
        StringEntity se = new StringEntity(o.toString())          
        httppost.setEntity(se);          
        response = httpclient.execute(httppost);
        
        // get response code
        StatusLine status = response.getStatusLine()
        int statusCode = status.getStatusCode()
        
        println statusCode
        if (statusCode == 201) {
            println 201 + " shopcart OK"
        }
        if (statusCode == 400) {
            println 400 + " shopcart FAILED"
        }
        
        return statusCode
  
    }
    
}
